from rest_framework import serializers
from article.models import *
from user.models import User
#频道主列标序列化器
class Channelserializer(serializers.ModelSerializer):
    class Meta:
        model=Channel
        fields=['id','name']
#用户自己的文章的序列化器
class Articlesimpleserializer(serializers.ModelSerializer):
    class Meta:
        model=Article
        fields=['id','title']
#用户嵌套序列化器
class Userserializer(serializers.ModelSerializer):
    articles=serializers.SerializerMethodField()

    # 钩子函数序列化,必须是以get_开头的
    def get_articles(self, obj):
        articles = obj.articles.filter(user_id=obj.id)
        articles= Articlesimpleserializer(articles, many=True)
        return articles.data
    class Meta:
        model=User
        fields=['id','username','avatar','articles','fans']

# labels
class labelssimpleserializer(serializers.ModelSerializer):
    articles = serializers.SerializerMethodField()
    # 钩子函数序列化,必须是以get_开头的
    def get_articles(self, obj):
        articles = obj.articles.filter(lables_id=obj.id)
        articles = Articlesimpleserializer(articles, many=True)
        return articles.data
    class Meta:
        model=Label
        fields='__all__'
#频道详情序列化器
class Articledeatilsserializer(serializers.ModelSerializer):
    user=Userserializer()
    # labels=serializers.SerializerMethodField()
    # def get_labels(self, obj):
    #     labels = obj.labels.filter(id=obj.id)
    #     labels= labelssimpleserializer(labels, many=True)
    #     return labels.data
    class Meta:
        model=Article
        fields='__all__'
#收藏序列化器
class Articlecollectserialieze(serializers.ModelSerializer):
    class Meta:
        model=Article
        fields=['collected_users']

class Cozerializer(serializers.ModelSerializer):
    class Meta:
        model=Comment
        fields = '__all__'
#评论简单序列化器
class Commzerializer(serializers.ModelSerializer):
    user=Userserializer()
    subs = Cozerializer(many=True)
    article = serializers.PrimaryKeyRelatedField(read_only=True)
    class Meta:
        model = Comment
        fields ='__all__'
#评论详细序列化器
class Commetserializer(serializers.ModelSerializer):
    user = Userserializer()
    subs=Commzerializer(many=True)
    article=serializers.PrimaryKeyRelatedField(read_only=True)
    class Meta:
        model=Comment
        fields='__all__'
#文章所有详情信息的序列化器
class ArticleAllInfoserializer(serializers.ModelSerializer):
    user=Userserializer()
    comments=Commetserializer(many=True)
    lables=serializers.PrimaryKeyRelatedField(read_only=True)
    collected_users=serializers.StringRelatedField()
    class Meta:
        model=Article
        fields='__all__'
class Lableserializer(serializers.ModelSerializer):
    class Meta:
        model=Label
        fields=['id','label_name']
#新建文章
class ArticlePubserialieze(serializers.ModelSerializer):
    labels=serializers.PrimaryKeyRelatedField(read_only=True)
    user_id=serializers.IntegerField()
    channel=serializers.PrimaryKeyRelatedField(read_only=True)
    class Meta:
        model=Article
        fields=['content','labels','title','channel','image','user_id']
#评论文章
class Commentserializer(serializers.ModelSerializer):
    article=serializers.PrimaryKeyRelatedField(read_only=True)
    subs=serializers.IntegerField()
    user_id=serializers.IntegerField()
    class Meta:
        model=Comment
        fields='__all__'
#搜索文章系列化
class Articleserchserializer(serializers.ModelSerializer):
    class Meta:
        model=Article
        fields=['id','createtime','content','title']